package com.itheima.web.controller.system;



import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import com.itheima.domain.system.LeaveWord;
import com.itheima.domain.system.LeaveWordExample;
import com.itheima.domain.system.User;
import com.itheima.service.system.DeptService;
import com.itheima.service.system.LeaveWordService;
import com.itheima.service.system.UserService;
import com.itheima.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/system/leaveWord")
public class LeaveWordController extends BaseController {

    @Reference
    private LeaveWordService leaveWordService;
    @Reference
    private UserService userService;
    @Reference
    private DeptService deptService;

    //前端定时器 刷新消息个数
    @RequestMapping(value = "/getNum",name = "定时刷新留言数量")
    @ResponseBody
    public int getNum(String name) {
        Integer i= 0 ;
        i =  leaveWordService.selectNum(name);
        System.out.println(i);

        return i;

    }
    //收件人下拉框需要的数据
    @RequestMapping(value = "/toName",name = "下拉框收件人")
    @ResponseBody
    public List<User> toName(String id) {
        List<User> userList = userService.findByDept(id);
        request.setAttribute("userList",userList);
        return userList;
    }

    @RequestMapping(value = "/list", name = "留言列表查询")
    public String list(
            @RequestParam(defaultValue = "1", name = "page") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        LeaveWordExample leaveWordExample = new LeaveWordExample();

        //1 封装条件
        LeaveWordExample.Criteria criteria = leaveWordExample.createCriteria();

        criteria.andCompanyIdEqualTo(getCompanyId());

        //判断用户等级,根据等级不同去拼接不同条件
        Integer degree = getUser().getDegree();
        if (degree == 4) {//普通员工
            criteria.andToNameEqualTo(getUser().getUserName());
        } else if (degree == 3) {//部门经理
            criteria.andDeptIdEqualTo(getUser().getDeptId());
        } else if (degree == 2) {//总经理
            criteria.andDeptIdLike(getUser().getDeptId() + "%");
        }

        //2 排序
        leaveWordExample.setOrderByClause("from_time  desc");

        PageInfo pageInfo = leaveWordService.findByPage(pageNum, pageSize, leaveWordExample);
        request.setAttribute("page", pageInfo);

        return "/system/leaveWord/leaveWord-list";
    }


    @RequestMapping(value = "/toAdd", name = "跳转留言新增页面")
    public String toAdd() {
        //部门下拉框 所需要的数据
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);
        //request.setAttribute("username",getUser().);
        //fromName
        request.setAttribute("fromName",getUser().getUserName());
        return "/system/leaveWord/leaveWord-add";
    }

    @RequestMapping(value = "/edit", name = "留言新增")
    public String edit(LeaveWord leaveWord) {
        if (StringUtils.isEmpty(leaveWord.getId())) {
            //1. 设置主键
            leaveWord.setId(UUID.randomUUID().toString());

            //2. 设置留言信息
            leaveWord.setCompanyId(getCompanyId());
            leaveWord.setCompanyName(getCompanyName());
            //发件id
            leaveWord.setFromId(getUser().getId());
            //收件人
            leaveWord.setToName(leaveWord.getToName());
            //收件人id

            //部门名
            leaveWord.setDeptName(getUser().getDeptName());
            //部门id
            leaveWord.setDeptId(getUser().getDeptId());

            //收发时间
            leaveWord.setFromTime(new Date());
            leaveWord.setToTime(new Date());
            //内容
            leaveWord.setText(leaveWord.getText());
            //存入域中
            request.setAttribute("leaveWord",leaveWord);
            //4. 设置当前留言的状态
            leaveWord.setStat(0);//未读
            leaveWordService.save(leaveWord);
        }else {
            leaveWordService.update(leaveWord);
            return "/system/leaveWord/leaveWord-view" ;
        }
        //重定向到list方法
        return "redirect:/system/leaveWord/list.do";
    }


    @RequestMapping(value = "/delete", name = "留言删除")
    public String delete(String id) {
        //调用service删除
        leaveWordService.delete(id);

        //重定向到list方法
        return "redirect:/system/leaveWord/list.do";
    }

        @RequestMapping(value = "/view", name = "留言查看")
        public String view(String id) {
            LeaveWord leaveWord = leaveWordService.findById(id);
            //如果收件人name 等于自己的 name  看发给自己的留言
            if(leaveWord.getToName().equals(getUser().getUserName())){
                leaveWord.setStat(1);
                leaveWordService.update(leaveWord);
                //
            }

            request.setAttribute("leaveWord",leaveWord);
            return "/system/leaveWord/leaveWord-view";

    }
    @RequestMapping(value = "/reply", name = "留言回复")
    public String reply(String id,String text) {
        //1. 根据发件人id 得到 留言对象
        LeaveWord leaveWord = leaveWordService.findById(id);

        //2.转为 回复对象
        //uuid
        leaveWord.setId(UUID.randomUUID().toString());

        // 老王-> 小北   需要一个中间变量
        //name  ->  老王
        String name = leaveWord.getFromName();
        //老王 -> 小北
        leaveWord.setFromName(leaveWord.getToName());
        //小北 -> 老王
        leaveWord.setToName(name);

        //发件人id变成收件人id
        leaveWord.setFromId(leaveWord.getToId());

        //收件人id 变成发件人 id
        leaveWord.setToId(leaveWord.getFromId());
        //发送时间 接收时间 new Date
        leaveWord.setFromTime(new Date());
        leaveWord.setToTime(new Date());
        //部门名 根据发件人(user)对象的name 找到他的部门name
        leaveWord.setDeptName(getUser().getDeptName());
        //部门id 根据发件人(user)对象的id 找到他的部门id
        leaveWord.setDeptId(getUser().getDeptId());
        //内容  回复的内容
        leaveWord.setText(text);
        //根据收件人id  找到user对象  获取 公司id  和 公司name
        User user = userService.findById(leaveWord.getToId());
        leaveWord.setCompanyId(user.getCompanyId());
        leaveWord.setCompanyName(user.getCompanyName());

        //4. 设置当前留言的状态
        leaveWord.setStat(0);//未读
        leaveWordService.save(leaveWord);

        return "redirect:/system/leaveWord/list.do";
    }

}
